function [PQ, status, scheduledTargets] = SCP_ScheduleRun_All(PQ, s1TargetList, s2TargetList)
%IA_ScheduleRun_SCP Simple Constraint Programming
% Detailed explanation goes here

scheduledTargets = [];
status = 0;
randomTargetList = randperm(PQ_Size(PQ));
randomTargetLists(1:PQ_Size(PQ), 1:PQ_Size(PQ)) = 0;

for n = 1:length(randomTargetList)
  randomTargetLists(n,:) = randomTargetList;
  randomTargetList = [randomTargetList(2:length(randomTargetList)) randomTargetList(1)];
end

for permutation = 1:size(randomTargetLists, 1)
  tempScheduledTargets = [];
  
  for priority = 1:PQ_Priorities(PQ)
    for target = 1:size(randomTargetLists, 2)
      [Task, status] = PQ_Fetch_Task(PQ, randomTargetLists(permutation, target), PQ_Priorities(PQ)+1-priority);
      if ~status
        continue;
      end
      target_id = Task.id;
      consistent = 1;
      
      for node = 1:length(tempScheduledTargets)
        node_id = tempScheduledTargets(node);
        if node_id ~= target_id
          [t_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == target_id));
          [n_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == node_id));

          if n_row1 < t_row1
            relation = IA_Relation_Less();
          elseif n_row1 == t_row1
            relation = IA_Relation_Dummy();
          else
            relation = IA_Relation_Greater();
          end

          [t_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == target_id));
          [n_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == node_id));

          if n_row2 < t_row2
            relation = IA_Relation_Intersect(relation, IA_Relation_Less());
          elseif n_row2 == t_row2
            relation = IA_Relation_Intersect(relation, IA_Relation_Dummy());
          else
            relation = IA_Relation_Intersect(relation, IA_Relation_Greater());
          end

          if IA_Relation_Is_Null(relation)
            consistent = 0;
            break;
          end
        end
      end
      
      if consistent
        tempScheduledTargets = [tempScheduledTargets target_id]; %#ok<AGROW>
      end
    end
  end
  
  % check for the best solution and use this one instead of the current....
  fitness = GRASP_Fitness(scheduledTargets);
  tempFitness = GRASP_Fitness(tempScheduledTargets);
  if tempFitness > fitness
    scheduledTargets = tempScheduledTargets;
  end
end

% Sort the scheduled targets by consulting the target lists per sensor
for node1 = 1:size(scheduledTargets, 2)-1
  for node2 = node1+1:size(scheduledTargets, 2)
    [s1Row1] = ST_FindTargetId2(s1TargetList, scheduledTargets(node1));
    [s1Row2] = ST_FindTargetId2(s1TargetList, scheduledTargets(node2));
    
    [s2Row1] = ST_FindTargetId2(s2TargetList, scheduledTargets(node1));
    [s2Row2] = ST_FindTargetId2(s2TargetList, scheduledTargets(node2));
    
    if (s1Row2 < s1Row1) || (s2Row2 < s2Row1)
      temp = scheduledTargets(node1);
      scheduledTargets(node1) = scheduledTargets(node2); %#ok<AGROW>
      scheduledTargets(node2) = temp; %#ok<AGROW>
    end
  end
end

if ~isempty(scheduledTargets)
  status = 1;
end
end %function